﻿using System;
using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class BattleRenderCloseUpController : BattleRenderBaseController
    {
        public bool active { get; private set; }
        
        protected override void OnDispose()
        {
            base.OnDispose();
            if (this.active)
            {
                CommonVideoPlayManager.Instance.StopVideo();
            }
            this.active = false;
        }
        
        public async UniTaskVoid Active(CloseUpParam param)
        {
            this.active = true;
            var rTimelineComp = param.unit.GetComponent<BattleTimelineComponent>(ETComponentType.BattleTimelineComponent);
            rTimelineComp.Pause();

            var rPath = PathConstant.GetUnitVideoPath(param.unit.roleName, param.video);
            await CommonVideoPlayManager.Instance.PlayVideo(rPath);
            
            CommonVideoPlayManager.Instance.StopVideo();
            this.DeActive(param);
        }

        private void DeActive(CloseUpParam param)
        {
            if (this.active)
            {
                var rTimelineComp = param.unit.GetComponent<BattleTimelineComponent>(ETComponentType.BattleTimelineComponent);
                rTimelineComp.JumpCloseUpEnd();
                this.active = false;
            }
        }
    }

    [Serializable]
    public struct CloseUpParam
    {
        public string video;
        
        public UnitView unit { get; set; }
    }
}